import 'package:core_package/cache/user_cache.dart';
import 'package:core_package/ui/widget/my_asset_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/model/province_model.dart';
import 'package:flutter_app_unionad/request/facility_request.dart';
import 'package:flutter_app_unionad/ui/basice/base_navigation_widget.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/ui/basice/input_separator_widget.dart';
import 'package:flutter_app_unionad/ui/basice/my_button.dart';
import 'package:flutter_app_unionad/ui/basice/my_separator_widget.dart';
import 'package:flutter_app_unionad/ui/basice/select_sex_widget.dart';
import 'package:flutter_app_unionad/ui/basice/select_title_and_detail_widget.dart';
import 'package:flutter_app_unionad/ui/basice/show_select_address_dialog.dart';

//预约安装
class MakeAnAppointmentInstallPage extends StatefulWidget {
  static const String routeName = "/makeAnAppointmentInstallPage";

  final bool needPhone;

  const MakeAnAppointmentInstallPage({Key? key, this.needPhone = false}) : super(key: key); //需要邀请人手机号--工作台进来

  @override
  _MakeAnAppointmentInstallPageState createState() => _MakeAnAppointmentInstallPageState();
}

class _MakeAnAppointmentInstallPageState extends State<MakeAnAppointmentInstallPage> {
  String? name;
  int sex = 1;
  String? phone;
  String? address;
  String? invitePhone;
  ProvinceModel? _provinceModel;
  ProvinceModel? _cityModel;
  ProvinceModel? _areaModel;

  @override
  Widget build(BuildContext context) {
    return buildContent();
  }

  Widget buildContent() {
    return Container(
      padding: EdgeInsets.symmetric(
        horizontal: 40.px,
        vertical: 40.px,
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(5.px),
      ),
      child: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "基本信息",
              style: TextStyle(
                color: MyColors.formHex("#272727FF"),
                fontSize: MyFont.font16,
                fontWeight: MyFontWeight.bold,
              ),
            ),
            MySizeBox(height: 30),
            InputSeparatorWidget(
              title: "姓名",
              placeholder: "请输入姓名",
              valueChange: (value) => name = value,
              controller: TextEditingController(text: name),
            ),
            MySizeBox(height: 10),
            SelectSexWidget(sex: sex, selectSexOnTap: (sex) => sex = sex),
            MySizeBox(height: 10),
            InputSeparatorWidget(
              title: "联系电话",
              placeholder: "请输入联系电话",
              valueChange: (value) => phone = value,
              controller: TextEditingController(text: phone),
              maxLength: 11,
              keyboard: TextInputType.number,
            ),
            MySizeBox(height: 10),
            SelectTitleAndDetailWidget(
                title: "选择区域",
                detail:
                    _provinceModel != null ? _provinceModel!.name! + _cityModel!.name! + _areaModel!.name! : "请选择区域",
                icon: Icon(Icons.arrow_drop_down, size: 15.px),
                onTap: () => selectCity()),
            MySizeBox(height: 10),
            InputSeparatorWidget(
              title: "详细地址",
              placeholder: "请输入详细地址",
              valueChange: (value) => address = value,
              controller: TextEditingController(text: address),
            ),
            Offstage(
              offstage: !widget.needPhone,
              child: MySizeBox(height: 10),
            ),
            Offstage(
              offstage: !widget.needPhone,
              child: InputSeparatorWidget(
                title: "邀请人",
                placeholder: "请输入邀请人手机号",
                valueChange: (value) => invitePhone = value,
                controller: TextEditingController(text: invitePhone),
                keyboard: TextInputType.number,
              ),
            ),
            MySizeBox(height: 40.px),
            Center(
              child: MyButton(
                title: "开启健康安全用水",
                width: 200.px,
                onTap: () {
                  //
                  saveInfo();
                },
              ),
            ),
            MySizeBox(height: 20.px),
          ],
        ),
      ),
    );
  }

  void selectCity() {
    ShowSelectAddressDialog.showAddress(context, onSelected: (province, city, area) {
      _provinceModel = province;
      _cityModel = city;
      _areaModel = area;
      setState(() {});
    });
  }

  void saveInfo() {
    if (name == null || name!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: "姓名不能为空");
      return;
    }

    print( widget.needPhone);
    if (phone == null || phone!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: "联系电话不能为空");
      return;
    }
    if (!isPhoneString(phone)) {
      ShowToastWidget.showToast(msg: "联系电话格式不对");
      return;
    }
    if (_provinceModel == null) {
      ShowToastWidget.showToast(msg: "请选择区域");
      return;
    }
    if (address == null || address!.stringIsEmpty) {
      ShowToastWidget.showToast(msg: "详细地址不能为空");
      return;
    }
    if ((invitePhone == null || invitePhone!.stringIsEmpty )&& widget.needPhone) {
      ShowToastWidget.showToast(msg: "邀请人电话不能为空");
      return;
    }

    ProgressDialog.showProgress(context);
    FacilityRequest.makeAnAppointment(
      sex: sex,
      userAddress: address,
      userTel: phone,
      userName: name,
      city: _cityModel!.name,
      userCityCode: _cityModel!.id.toString(),
      userProvince: _provinceModel!.name,
      userProvinceCode: _provinceModel!.id.toString(),
      userCounty: _areaModel!.name,
      userCountyCode: _areaModel!.id.toString(),
      invitePhone: widget.needPhone ? invitePhone : null,
    ).then((value) {
      ProgressDialog.dismiss(context);
      if (value) {
        UserCacheUil.setBool(Configuration.makeAnInstall, true);
        if (widget.needPhone) {
          if (mounted) {
            setState(() {
              sex = 1;
              address = null;
              name = null;
              _cityModel = null;
              _provinceModel = null;
              _areaModel = null;
              invitePhone = null;
              phone = null;
            });
          }
        } else
          Navigator.pop(context, true);
      }
    });
  }
}
